package com.doone.telmarket.action.web;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.action.admin.BaseActionSupport;
import com.doone.telmarket.domain.Activity;
import com.doone.telmarket.domain.OrderSort;
import com.doone.telmarket.domain.ServiceProductView;
import com.doone.telmarket.domain.ibator.ServiceType;
import com.doone.telmarket.service.ActivityOperation;
import com.doone.telmarket.service.ServiceProductOperation;
import com.doone.telmarket.service.ServiceTypeOperation;
import com.doone.telmarket.vo.HotBusiVo;
import com.opensymphony.xwork2.ActionContext;

public class QueryServiceAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(QueryServiceAction.class);

	@Autowired
	private ServiceProductOperation remoteServiceProductOperation;

	private InputStream inputStream;

	private String serviceTypeName;

	private String errorMsg;

	private String type;

	@Autowired
	private ActivityOperation remoteActivityOperation;

	@Autowired
	private ServiceTypeOperation remoteServiceTypeOperation;

	private List<ServiceProductView> pageList = new ArrayList<ServiceProductView>();

	@Action(value = "queryOrderSort", results = { @Result(name = "success", type = "stream", params = { "contentType",
			"text/html;charset=UTF-8", "inputName", "inputStream" }) })
	public String queryOrderSort() {
		try {
			String tab = "";
			List<OrderSort> proList = remoteServiceProductOperation.selectOrderSort();

			for (int i = 0; i < proList.size(); i++) {
				tab += "<table width='160' border='0' cellspacing='0' cellpadding='0'><tr>";
				if (i % 2 == 0) {
					tab += "<td width='28' height='24' align='center' valign='middle'>";
				} else {
					tab += "<td width='28' height='24' align='center' valign='middle'  bgcolor='#f0f3ea'>";
				}
				if (i == 0) {
					tab += "<img src='/telmarket-web/images/dels/img_8.gif' width='14' height='11' />";
				} else if (i == 1) {
					tab += "<img src='/telmarket-web/images/dels/img_9.gif' width='14' height='11' />";
				} else if (i == 2) {
					tab += "<img src='/telmarket-web/images/dels/img_10.gif' width='14' height='11' />";
				} else if (i == 3) {
					tab += "<img src='/telmarket-web/images/dels/img_11.gif' width='14' height='11' />";
				} else if (i == 4) {
					tab += "<img src='/telmarket-web/images/dels/img_12.gif' width='14' height='11' />";
				} else if (i == 5) {
					tab += "<img src='/telmarket-web/images/dels/img_13.gif' width='14' height='11' />";
				} else if (i == 6) {
					tab += "<img src='/telmarket-web/images/dels/img_14.gif' width='14' height='11' />";
				} else if (i == 7) {
					tab += "<img src='/telmarket-web/images/dels/img_15.gif' width='14' height='11' />";
				} else if (i == 8) {
					tab += "<img src='/telmarket-web/images/dels/img_16.gif' width='14' height='11' />";
				} else if (i == 9) {
					tab += "<img src='/telmarket-web/images/dels/img_17.gif' width='14' height='11' />";
				}
				tab += "</td>";
				if (i % 2 == 0) {
					tab += "<td width='74' align='left' class='huise_12'>";
				} else {
					tab += "<td width='74' align='left' class='huise_12'  bgcolor='#f0f3ea'>";
				}
				tab += "<a href='/telmarket-web" + proList.get(i).getProductPage() + "' target='_parent'>"
						+ proList.get(i).getProductName() + "</a></td>";
				if (i % 2 == 0) {
					tab += "<td width='58' align='right' valign='middle' class='huise_12' >";
				} else {
					tab += "<td width='58' align='right' valign='middle' class='huise_12'  bgcolor='#f0f3ea'>";
				}
				tab += "<a href='/telmarket-web" + proList.get(i).getProductPage() + "' target='_parent'>"
						+ Integer.parseInt(proList.get(i).getProductFee()) / 100 + "元/月</a>";
				tab += "</td></tr></table>";
			}

			inputStream = new ByteArrayInputStream(tab.getBytes("UTF-8"));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				inputStream = new ByteArrayInputStream("获取数据失败".getBytes("UTF-8"));
				return SUCCESS;
			} catch (IOException e1) {
				e1.printStackTrace();
				return SUCCESS;
			}
		}

	}

	/**
	 * @package
	 * @author 甘雨波
	 * @description 获取分页数据
	 * @date 2009-11-2
	 * @return
	 */
	@Action(value = "getStartPage", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "list1", location = "/web/common/list_1.jsp"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String getStartPage() {
		try {

			log.info("serviceTypeName==" + serviceTypeName);

			//	serviceTypeName = new String(serviceTypeName.getBytes("ISO-8859-1"), "UTF-8");
			int maxPage = remoteServiceProductOperation.getMaxPage(serviceTypeName);
			session.put("maxPage", maxPage);
			pageList = remoteServiceProductOperation.getNextPage(1, serviceTypeName);
			session.put("pageNumber", 1);
			return "list1";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "很抱歉,获取数据失败";
			return "error";
		}
	}

	/**
	 * @package
	 * @author 甘雨波
	 * @description 取上一页
	 * @date 2009-11-2
	 * @return
	 */
	@Action(value = "getPriviPage", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "list1", location = "/web/common/list_1.jsp"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String getPriviPage() {
		try {
			int pageNumber = (Integer) session.get("pageNumber");
			pageNumber--;
			//serviceTypeName = new String(serviceTypeName.getBytes("ISO-8859-1"), "UTF-8");
			pageList = remoteServiceProductOperation.getPriviPage(pageNumber, serviceTypeName);
			if (pageNumber < 1) {
				session.put("pageNumber", 1);
			} else {
				session.put("pageNumber", pageNumber);
			}

			return "list1";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "很抱歉,获取数据失败";
			return "error";
		}
	}

	/**
	 * @package
	 * @author 甘雨波
	 * @description 取下一页
	 * @date 2009-11-2
	 * @return
	 */
	@Action(value = "getNextPage", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "list1", location = "/web/common/list_1.jsp"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String getNextPage() {
		try {
			int pageNumber = (Integer) session.get("pageNumber");
			pageNumber++;
			//serviceTypeName = new String(serviceTypeName.getBytes("ISO-8859-1"), "UTF-8");
			int maxPage = remoteServiceProductOperation.getMaxPage(serviceTypeName);
			pageList = remoteServiceProductOperation.getNextPage(pageNumber, serviceTypeName);
			if (pageNumber > maxPage) {
				session.put("pageNumber", maxPage);
			} else {
				session.put("pageNumber", pageNumber);
			}
			return "list1";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "很抱歉,获取数据失败";
			return "error";
		}
	}

	/**
	 * @package
	 * @author 甘雨波
	 * @description 取最后一页
	 * @date 2009-11-2
	 * @return
	 */
	@Action(value = "getEndPage", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "list1", location = "/web/common/list_1.jsp"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String getEndPage() {
		try {

			//serviceTypeName = new String(serviceTypeName.getBytes("ISO-8859-1"), "UTF-8");
			int maxPage = remoteServiceProductOperation.getMaxPage(serviceTypeName);
			pageList = remoteServiceProductOperation.getEndPage(serviceTypeName);
			session.put("pageNumber", maxPage);
			return "list1";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "很抱歉,获取数据失败";
			return "error";
		}
	}

	@Action("queryHeadServiceType")
	public void queryHeadServiceType() {
		try {
			StringBuffer sb = new StringBuffer();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getServerName());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append(sr.getContextPath());

			List<ServiceType> typeList = remoteServiceTypeOperation.selectAllServiceType();
			sb.append("<table height=\"21\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"Baise_121\"><tr>");

			for (int i = 0; i < typeList.size(); i++) {
				ServiceType st = typeList.get(i);
				sb.append("<td width=\"110\" align=\"center\" style=\"padding-top:2px;\"><a onclick=\"submitForm('"+st.getServiceTypeName()+"');\" style=\"color:white;cursor: hand\" >" + st.getServiceTypeName() + "</a></td>");
				sb.append("<td><img src=\"" + pagePath.toString()
						+ "/images/top/dh1.gif\" width=\"18\" height=\"11\" /></td>");
				if(i==(typeList.size()-1)){
					sb.append("<td width=\"110\" align=\"center\" style=\"padding-top:2px;\"><a href=\""
							+ pagePath.toString() + "/web/common/list_5.jsp"
							+ "\" target=\"_parent\">天翼空间</a></td>");
					sb.append("<td><img src=\"" + pagePath.toString()
							+ "/images/top/dh1.gif\" width=\"18\" height=\"11\" /></td>");
				}
			}
			if (typeList.size() == 0) {
				sb.append("<td>没有数据</td>");
			}
			sb.append("</tr></table>");
			responseWrite(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@SuppressWarnings("static-access")
	@Action("getHotBusiByType")
	public void getHotBusiByType() {
		try {
			List<HotBusiVo> list = remoteServiceProductOperation.selectHotBusi(type);
			JSONUtil json = new JSONUtil();
			log.info("HEADHOTBUSI" + list.size());
			responseWrite(json.serialize(list));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action(value = "selectActivity", results = { @Result(name = "success", location = "/web/common/list_4.jsp"),
			@Result(name = "error", location = "/web/error.jsp") })
	public String selectActivity() {
		try {
			//log.info("查询精彩促销");

			List<Activity> list = remoteActivityOperation.selectActivity();
			//log.info("查询精彩促销"+list.size());
			session.put("list", list);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = "很抱歉,获取数据失败";
			return "error";
		}

	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getServiceTypeName() {
		return serviceTypeName;
	}

	public void setServiceTypeName(String serviceTypeName) {
		this.serviceTypeName = serviceTypeName;
	}

	public List<ServiceProductView> getPageList() {
		return pageList;
	}

	public void setPageList(List<ServiceProductView> pageList) {
		this.pageList = pageList;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
